package com.health.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.manage.mapper.MessageMapper;
import com.health.manage.mapper.UserMapper;
import com.health.manage.pojo.dto.MessageDto;
import com.health.manage.pojo.entity.Message;
import com.health.manage.pojo.entity.User;
import com.health.manage.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @Description: 聊天信息业务处理
 * @author: message
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

	@Autowired(required = false)
	private MessageMapper messageMapper;
	@Autowired(required = false)
	private UserMapper userMapper;

	@Override
	public Boolean addData(Message message) {
		int result = this.messageMapper.insert(message);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean updateData(Message message) {
		int result = this.messageMapper.updateById(message);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean deleteData(Long id) {
		int result = this.messageMapper.deleteById(id);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page<Message> getPageData(MessageDto messageDto) {

		Page<Message> page = new Page<>(messageDto.getCurrentPage(), messageDto.getPageSize());
		QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
		if(messageDto.getToContactId()!=null && !"".equals(messageDto.getToContactId())){
			queryWrapper.eq("toContactId", messageDto.getToContactId());
		}
		if(messageDto.getFromUserId()!=null && !"".equals(messageDto.getFromUserId())){
			queryWrapper.eq("fromUserId", messageDto.getFromUserId());
		}
		Page<Message>records = this.messageMapper.selectPage(page, queryWrapper);
		for(Message message : records.getRecords()){
			User user = this.userMapper.selectById(message.getToContactId());
			if(user != null){
				message.setToContactUser(user);
			}
			User creator = this.userMapper.selectById(message.getFromUserId());
			if(creator != null){
				//message.setCreator(creator);
			}
		}
		return records;
	}


}
